package green;

/**
 * @author ST_JG
 * @version 1.0
 * 关于列表反转的错误，网页会出现
 * java.lang.NullPointerException: Cannot read field "val" because "<parameter1>" is null
 *   at line 19, Solution.f
 *   at line 13, Solution.reverseList
 *   at line 54, __DriverSolution__.__helper__
 *   at line 84, __Driver__.main
 */
public class Solution {
    public static void main(String[] args) {

        ListNode node5 = new ListNode(5,null);
        ListNode node4 = new ListNode(4,node5);
        ListNode node3 = new ListNode(3,node4);
        ListNode node2 = new ListNode(2,node3);
        ListNode head = new ListNode(1,node2);

        ListNode new_Like = f(head);
    }


    public static ListNode f(ListNode p){
        if(p == null || p.next == null){
            //为什么此处的p.val在网页上会报NullPointException，debug没找到
            return new ListNode(p.val);
        }
        ListNode new_List = f(p.next);
        //此处逻辑有误
        //new_List.next = new ListNode(p.val);
        //此处还要遍历新数组
        ListNode temp = new_List;
        while(temp.next != null){
            temp = temp.next;
        }
        temp.next = new ListNode(p.val);
        return new_List;
    }
}

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
